<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
/**
* A DHTML menu component for joomla
* @version 1.6.2
* @package ngmenufree
* @copyright (C) 2004 - 2012 by Georg Lorenz
* @license Released under the terms of the GNU General Public License
**/

class com_ngmenuInstallerScript
{
	private $release = '1.8.0';
    private $minimum_joomla_release = '1.6.0';    

	/**
	 * method to install the component
	 *
	 * @return void
	 */
	function install($parent) 
	{
		$this->removeDefaultModuleRecord();

		// $parent is the class calling this method
		$parent->getParent()->setRedirectURL('index.php?option=com_ngmenu');
	}
 
	/**
	 * method to uninstall the component
	 *
	 * @return void
	 */
	function uninstall($parent) 
	{
		// $parent is the class calling this method
//		echo '<p>' . JText::_('COM_NGMENU_UNINSTALL_TEXT') . '</p>';
	}
 
	/**
	 * method to update the component
	 *
	 * @return void
	 */
	function update($parent) 
	{
		// $parent is the class calling this method
		$this->removeDefaultModuleRecord();

		echo '<p>' . JText::_('COM_NGMENU_UPDATE_TEXT') . '</p>';
	}
 
	/**
	 * method to run before an install/update/uninstall method
	 *
	 * @return void
	 */
	function preflight($type, $parent) 
	{
		// $parent is the class calling this method
		// $type is the type of change (install, update or discover_install)
		$jversion = new JVersion();
 
		// Extract the version number from the manifest. 
		$this->release=$parent->get("manifest")->version;
 
		// Find mimimum required joomla version
		$this->minimum_joomla_release=$parent->get("manifest")->attributes()->version;    
 
		if( version_compare( $jversion->getShortVersion(), $this->minimum_joomla_release, 'lt' ) ) {
			Jerror::raiseWarning(null, 'Cannot install NGMenu Component in a Joomla release prior to '.$this->minimum_joomla_release);
			return false;
		}
 
		// abort if the component being installed is not newer than the currently installed version
		if ( $type == 'update' ) {
			$oldRelease = $this->getParam('version');
			$rel = $oldRelease . ' ' . JText::_('COM_NGMENU_PREFLIGHT_UPDATE_TO') . ' ' . $this->release;
			if ( version_compare( $this->release, $oldRelease, 'le' ) ) {
				Jerror::raiseWarning(null, 'Incorrect version sequence. Cannot upgrade ' . $rel);
				return false;
			}
		}
		else { $rel = $this->release; }
		
		if ( $type == 'install' ) {
			$db			= JFactory::getDbo();
//			$installer	= JInstaller::getInstance();

//			$db->setQuery("SELECT * FROM #__extensions WHERE element = 'com_ngmenufree'");
//			$id = $db->loadResult();
//			if($id){
//				$result = $installer->uninstall('component', $id);
//			}
			
//			$db->setQuery("SELECT * FROM #__extensions WHERE element = 'mod_ngmenufree'");
//			$id = $db->loadResult();
//			if($id){
//				$result = $installer->uninstall('module', $id);
//			}
			
			$db->setQuery("DROP TABLE IF EXISTS #__ngmenu_fonts");
			$db->query();
			
		}
		
		echo '<p>' . JText::_('COM_NGMENU_PREFLIGHT_' . strtoupper($type)) . ' ' . $rel . '</p>';
	}
 
	/**
	 * method to run after an install/update/uninstall method
	 *
	 * @return void
	 */
	function postflight($type, $parent) 
	{
		// $parent is the class calling this method
		// $type is the type of change (install, update or discover_install)
//		echo '<p>' . JText::_('COM_NGMENU_POSTFLIGHT_' . $type . '_TEXT') . '</p>';
	}

	/*
	 * get a variable from the manifest file (actually, from the manifest cache).
	 */
	function getParam( $name ) {
		$db = JFactory::getDbo();
		$db->setQuery('SELECT manifest_cache FROM #__extensions WHERE element = "com_ngmenu"');
		$manifest = json_decode( $db->loadResult(), true );
		return $manifest[ $name ];
	}

	/*
	 * sets parameter values in the component's row of the extension table
	 */
	 function setParams($param_array) {
		if ( count($param_array) > 0 ) {
			// read the existing component value(s)
			$db = JFactory::getDbo();
			$db->setQuery('SELECT params FROM #__extensions WHERE element = "com_ngmenu"');
			$params = json_decode( $db->loadResult(), true );
			// add the new variable(s) to the existing one(s)
			foreach ( $param_array as $name => $value ) {
				$params[ (string) $name ] = (string) $value;
			}
			// store the combined new and existing values back as a JSON string
			$paramsString = json_encode( $params );
			$db->setQuery('UPDATE #__extensions SET params = ' .
				$db->quote( $paramsString ) .
				' WHERE element = "com_ngmenu"' );
				$db->query();
		}
	}
	
	function removeDefaultModuleRecord() {
	
		$database =& JFactory::getDBO();
		$query = $database->getQuery(true);
		$query->delete('#__modules');
		$query->where("module='mod_ngmenu'");
		$query->where("title='NGMenu'");
		$database->setQuery($query);
		$database->query();
	}
}
